package springboot.test15.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import springboot.test15.bean.User;

import javax.servlet.http.HttpSession;

@Slf4j
@Controller
public class indexController {
    //登录界面
    @GetMapping(value={"/","/login"})
    public String loginPage(){
        return "login";
    }
    //登录页面 登陆成功重定向到main.html
    @PostMapping("/login")
    public String index(User user, HttpSession session, Model model){
        //当用户名不为空用户密码有长度时判断登陆成功
        if(!StringUtils.isEmpty(user.getUserName())&&"123".equals(user.getPassword())){
            session.setAttribute("loginUser",user);
            //这样就可以实现刷新不用重复提交表单
            return "redirect:/main.html";
        }
        else{
            model.addAttribute("message","帐号或密码错误");
            return "login";
        }
    }
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @GetMapping("/main.html")
    public String indexPage(Model model){
        ValueOperations<String,String> operations = stringRedisTemplate.opsForValue();
        String countMain = operations.get("/main.html");
        String countTable = operations.get("/dynamic_table");
        model.addAttribute("countMain",countMain);
        model.addAttribute("countTable",countTable);
        return "main";
    }

}
